#!/usr/bin/php
<?php
/*
 * Make a website. Yay!!
 * 
 * author: David Palmer <blinder.dave@gmail.com>
 */
main($argv);

function main($args) {
	if (!$args[1]) {
		useage();
	}
	
	$config = parse_ini_file($args[1]);
	
	if (! file_exists($config['path'])) {
		mkdir($config['path']);
	}
	
	// copy our dist files/directories to our path
	copyr("ref_app/",$config['path']);
	
	// lets drop in our framework classes
	$package = preg_replace('/\./','/',$config['package_name']);
	mkdir($config['path']."/classes/".$package);
	mkdir($config['path']."/classes/".$package."/actions");
	mkdir($config['path']."/classes/".$package."/interfaces");
	mkdir($config['path']."/classes/".$package."/daos");
	mkdir($config['path']."/classes/".$package."/daos/impls");
	mkdir($config['path']."/classes/".$package."/daos/interfaces");
	mkdir($config['path']."/classes/".$package."/utils");
	
	// lets add our virtual host :)
	if (! (file_exists($config['apache_conf']))) {
		die("Sorry I couldn't find: " . $config['apache_conf'].
			" Make sure this points to the apache configuration file\n" .
			" where you want to define virtual hosts!\n");
	} else {
		$afh = fopen($config['apache_conf'],'a');
		$vhost = "\n<VirtualHost *:80>\n" .
				"\tServerName " . $config['vhost_name'] . "\n".
				"\tDocumentRoot \"". $config['path'] . "/webroot\"\n".
				"\tDirectoryIndex controller.php\n".
				"\tErrorLog logs/" . $config['vhost_name'] ."-error.log\n".
				"\tCustomLog logs/" . $config['vhost_name']."-access.log common\n".
				"\t<Directory \"".$config['path']."/webroot\">\n".
				"\t\tAllowOverride all\n".
				"\t\tOptions All\n".
				"\t\tallow from all\n".
				"\t</Directory>\n".
				"</VirtualHost>\n\n";
		fwrite($afh,$vhost);
		fclose($afh);
		print($config['apache_conf'] . " has been modified with your new virtual host\n");
		print ("It'd be a very good idea to add this to your hosts file:\n");
		print("\t\t 127.0.0.1\t". $config['vhost_name'] . "\n");
	}
	
	print("site created!");
	
	return;
	
}
function useage() {
	print("./make-site.php config-file.ini\n");
	die();
}
/**
 * Copy a file, or recursively copy a folder and its contents
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.0.1
 * @link        http://aidanlister.com/repos/v/function.copyr.php
 * @param       string   $source    Source path
 * @param       string   $dest      Destination path
 * @return      bool     Returns TRUE on success, FALSE on failure
 */
function copyr($source, $dest)
{
    // Check for symlinks
    if (is_link($source)) {
        return symlink(readlink($source), $dest);
    }

    // Simple copy for a file
    if (is_file($source)) {
        return copy($source, $dest);
    }

    // Make destination directory
    if (!is_dir($dest)) {
        mkdir($dest);
    }

    // Loop through the folder
    $dir = dir($source);
    while (false !== $entry = $dir->read()) {
        // Skip pointers
        if ($entry == '.' || $entry == '..') {
            continue;
        }

        // Deep copy directories
        copyr("$source/$entry", "$dest/$entry");
    }

    // Clean up
    $dir->close();
    return true;
}

?>